package com.one.customer.controller;

import com.one.customer.model.Customer;
import com.one.customer.service.CurstomerService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/login.do")
public class CurstomerLogniController extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        //获取服务层对象
        CurstomerService curstomerService = new CurstomerService();
        //判断账号密码是否能查询到会员对象
        Customer customer = curstomerService.login(username, password);
        if (customer != null) {
            //获取session对象
            HttpSession session = req.getSession();
            //存储会员编号
            session.setAttribute("customerId", customer.getId());
            //存储用户账号
            session.setAttribute("username", customer.getUser_name());
            //存储用户电话号码
            session.setAttribute("phone", customer.getMobile());
            //页面转发：将请求发送到另一个逻辑（页面）
            req.getRequestDispatcher("/product_list.jsp").forward(req, resp);
        }else {
            req.getRequestDispatcher("/login.jsp").forward(req, resp);
        }
    }
}
